/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package net.etrs.formathlon.metier.service.uc6.authentificationLDAP;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.UserDetails;

/**
 * Classe qui va être mappée avec le context envoyé de l'Active
 * Directory de l'ETRS lors d'une authentification réussie.
 * @author OPIF
 */
public class ADTUserDetails implements UserDetails {
    //#######################################
    //##             Attributs             ##
    //#######################################
    
    // Attributs necessaires d'UserDetails
    // Nom qui sera tapé pour que l'utilisateur se connect
    private String username;
    private String password;
    private boolean accountNonExpired = true;
    private boolean accountNonLocked = true;
    private boolean credentialsNonExpired = true;
    private boolean enable = true;
    Collection<? extends GrantedAuthority> authorities = AuthorityUtils.NO_AUTHORITIES;
    ;
    
    // Attributs personnalisés
    //Correspond au champ ldap uid
    private String login;
    private String mail;
    private String titre;
    private List<String> cn = new ArrayList<String>();
    // Correspond au champs ldap sn
    private String nom;
    // Correspond au champs ldap givenName
    private String prenom;
    // Correspond au champs ldap departmentNumber
    private String entite;
    // Correspond au champs ldap initials
    private String grade;

    //#######################################
    //##         GETTER ET SETTER          ##
    //#######################################
      @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return authorities;
    }

    @Override
    public String getPassword() {
        return password;
    }

    @Override
    public String getUsername() {
        return username;
    }

    @Override
    public boolean isAccountNonExpired() {
        return accountNonExpired;
    }

    @Override
    public boolean isAccountNonLocked() {
        return accountNonLocked;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return credentialsNonExpired;
    }

    @Override
    public boolean isEnabled() {
        return enable;
    }

    /**
     * 
     * @param accountNonExpired 
     */
    public void setAccountNonExpired(boolean accountNonExpired) {
        this.accountNonExpired = accountNonExpired;
    }

    /**
     * 
     * @param accountNonLocked 
     */
    public void setAccountNonLocked(boolean accountNonLocked) {
        this.accountNonLocked = accountNonLocked;
    }
    
    /**
     * 
     * @param authorities 
     */
    public void setAuthorities(Collection<? extends GrantedAuthority> authorities) {
        this.authorities = authorities;
    }
    
    /**
     * 
     * @param credentialsNonExpired 
     */
    
    public void setCredentialsNonExpired(boolean credentialsNonExpired) {
        this.credentialsNonExpired = credentialsNonExpired;
    }

    /**
     * 
     * @param password 
     */
    public void setPassword(String password) {
        this.password = password;
    }
    
    /**
     * 
     * @param username 
     */
    public void setUsername(String username) {
        this.username = username;
    }

    /**
     * 
     * @return cn
     */
    public String[] getCn() {
        return cn.toArray(new String[cn.size()]);
    }

    /**
     * 
     * @param cn 
     */
    public void setCn(String[] cn) {
        this.cn = Arrays.asList(cn);
    }

    /**
     * 
     * @return enable
     */
    public boolean isEnable() {
        return enable;
    }

    /**
     * 
     * @param enable 
     */
    public void setEnable(boolean enable) {
        this.enable = enable;
    }

    /**
     * 
     * @return entite
     */
    public String getEntite() {
        return entite;
    }

    /**
     * 
     * @param entite 
     */
    public void setEntite(String entite) {
        this.entite = entite;
    }

    /**
     * 
     * @return grade
     */
    public String getGrade() {
        return grade;
    }

    /**
     * 
     * @param grade 
     */
    public void setGrade(String grade) {
        this.grade = grade.toUpperCase();
    }

    /**
     * 
     * @return login
     */
    public String getLogin() {
        return login;
    }

    /**
     * 
     * @param login 
     */
    public void setLogin(String login) {
        this.login = login;
    }

    /**
     * 
     * @return mail
     */
    public String getMail() {
        return mail;
    }

    /**
     * 
     * @param mail 
     */
    public void setMail(String mail) {
        this.mail = mail;
    }

    /**
     * 
     * @return prenom
     */
    public String getPrenom() {
        return prenom;
    }

    /**
     * 
     * @param prenom 
     */
    public void setPrenom(String prenom) {
        this.prenom = StringUtils.capitalize(prenom.toLowerCase());
    }

    /**
     * 
     * @return nom
     */
    public String getNom() {
        return nom;
    }

    /**
     * 
     * @param nom 
     */
    public void setNom(String nom) {
        this.nom = nom.toUpperCase();
    }

    /**
     * 
     * @return titre
     */
    public String getTitre() {
        return titre;
    }

    /**
     * 
     * @param titre 
     */
    public void setTitre(String titre) {
        this.titre = titre;
    }

    /**
     * Redéfinition du ToString pour certain affichage
     * @return un string formaté avec le grade nom prenom
     */
    @Override
    public String toString() {
        return String.format("%s %s %s", grade,nom,prenom);
    }


    
    
}
